package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Adres;
import hibernate.service.EmmyService;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("AdresBean")
@Scope("session")
public class AdresBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Adres currentAdres;	
	private List<Adres> adresList;
	
	@SuppressWarnings("unchecked")
	@Override
	@PostConstruct
	public void init(){		
		adresList = (List<Adres>) emmyService.findAll(new Adres());		
	}	
	
	@Override
	public String restart(){
		init();
		return "list";
	}
	
	@Override
	public String deleteView(){
		emmyService.delete(currentAdres.getAdresid());
		init();
		return "list";
	}
	
	@Override
	public String viewNew(){
		currentAdres= new Adres();
		return "detail";
	}
		
	@Override
	public String viewDetail(){		/*			
		List<Adres> valueList = (List<Adres>) adresTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<Object> keys = adresTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentAdres= valueList.get(((SimpleRowKey) keys.next()).intValue());
			System.out.println("dolu = "+currentAdres.getTckimlik());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		} */return "detay";
	}  
	
	@Override
	public String SaveOrUpdate() throws MyException{
		emmyService.saveOrUpdate(currentAdres);			
		init();
		return "list";
	}

	public String gitAdres(ActionEvent actionEvent){  
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "System Info", "Process Completed...");          
        FacesContext.getCurrentInstance().addMessage(null, message);         
        return "detay";
	}
	@SuppressWarnings("unchecked")
	public String getDetayliAdres(){		
		adresList = (List<Adres>) emmyService.getQuery(createQuery());
		return "list";
	}
	
	@Override
	public String createQuery(){
		Adres adr  = currentAdres;		
		String query = "from Adres";				
		
		if(adr == null){
			return query;
		}
		if(adr.getGrupid() != 0){
			if(query.indexOf("where") != -1){
				query += " and grupid="+adr.getGrupid()+"";
				
			}
			else{
				query += " where grupid="+adr.getGrupid()+"";			
			}
		}
		if(adr.getIl() != null){
			if(query.indexOf("where") != -1){
				query += " and il like '%"+adr.getIl()+"%'";
				
			}
			else{
				query += " where il like '%"+adr.getIl()+"%'";			
			}
		}
		
		if(adr.getIlce() != null){
			if(query.indexOf("where") != -1){
				query += " and ilce like '%"+adr.getIlce()+"%'";
				
			}
			else{
				query += " where ilce like '%"+adr.getIlce()+"%'";			
			}
		}
		if(adr.getMahalle() != null){
			if(query.indexOf("where") != -1){
				query += " and mahalle like '%"+adr.getMahalle()+"%'";
				
			}
			else{
				query += " where mahalle like '%"+adr.getMahalle()+"%'";			
			}
		}
		if(adr.getSokak() != null){
			if(query.indexOf("where") != -1){
				query += " and sokak like '%"+adr.getSokak()+"%'";
				
			}
			else{
				query += " where sokak like '%"+adr.getSokak()+"%'";			
			}
		}
		if(adr.getNumara() != null){
			if(query.indexOf("where") != -1){
				query += " and numara= "+adr.getNumara()+"";
				
			}
			else{
				query += " where numara= "+adr.getNumara()+"";			
			}
		}
		return query;
	}

	public Adres getCurrentAdres() {
		return currentAdres;
	}

	public void setCurrentAdres(Adres currentAdres) {
		this.currentAdres = currentAdres;
	}

	public List<Adres> getAdresList() {
		return adresList;
	}

	public void setAdresList(List<Adres> adresList) {
		this.adresList = adresList;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}
}